package com.ks.app.navigate.util;

import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.ks.app.navigate.hibernate.KsNavigate;
import com.ks.app.navigate.hibernate.KsWebsite;


public class JsoupUtil {
	
	
	/**
	 * 抓取导航类型
	 * @return
	 */
	public static List<KsNavigate> crawlGuideType(){
		//娱乐休闲  id = 1
		String content1 =
			"<li><a href=\"wap_club_37.html\" title=\"手机社区WAP导航\">社区</a></li>"+
			"<li><a href=\"wap_novel_2.html\" title=\"手机小说WAP导航\">小说</a></li>"+
			"<li><a href=\"wap_game_1.html\" title=\"手机游戏WAP导航\">游戏</a></li>"+
			"<li><a href=\"wap_constellat_10.html\" title=\"手机星座WAP导航\">星座</a></li>"+
			"<li><a href=\"wap_music_14.html\" title=\"手机音乐WAP导航\">音乐</a></li>"+
			"<li><a href=\"wap_pics_15.html\" title=\"手机图片WAP导航\">图片</a></li>"+
			"<li><a href=\"wap_anime_16.html\" title=\"手机动漫WAP导航\">动漫</a></li>"+
			"<li><a href=\"wap_friend_17.html\" title=\"手机交友WAP导航\">交友</a></li>"+
			"<li><a href=\"wap_fun_18.html\" title=\"手机搞笑WAP导航\">搞笑</a></li>"+
			"<li><a href=\"wap_entertainm_19.html\" title=\"手机娱乐WAP导航\">娱乐</a></li>"+
			"<li><a href=\"wap_travel_21.html\" title=\"手机旅游WAP导航\">旅游</a></li>"+
			"<li><a href=\"wap_video_42.html\" title=\"手机视频WAP导航\">视频</a></li>"+
			"<li><a href=\"wap_msg_44.html\" title=\"手机短信WAP导航\">短信</a></li>";
		//生活服务 id = 2
	   String content2 =	
			"<li><a href=\"wap_portal_39.html\" title=\"手机门户WAP导航\">门户</a></li>"+
			"<li><a href=\"wap_news_4.html\" title=\"手机新闻WAP导航\">新闻</a></li>"+
			"<li><a href=\"wap_military_7.html\" title=\"手机军事WAP导航\">军事</a></li>"+
			"<li><a href=\"wap_sports_8.html\" title=\"手机体育WAP导航\">体育</a></li>"+
			"<li><a href=\"wap_finance_9.html\" title=\"手机财经WAP导航\">财经</a></li>"+
			"<li><a href=\"wap_phone_12.html\" title=\"手机手机WAP导航\">手机</a></li>"+
			"<li><a href=\"wap_bank_13.html\" title=\"手机银行WAP导航\">银行</a></li>"+
			"<li><a href=\"wap_search_20.html\" title=\"手机搜索WAP导航\">搜索</a></li>"+
			"<li><a href=\"wap_lottery_22.html\" title=\"手机彩票WAP导航\">彩票</a></li>"+
			"<li><a href=\"wap_demand_23.html\" title=\"手机查询WAP导航\">查询</a></li>"+
			"<li><a href=\"wap_car_24.html\" title=\"手机汽车WAP导航\">汽车</a></li>"+
			"<li><a href=\"wap_estate_25.html\" title=\"手机房产WAP导航\">房产</a></li>"+
			"<li><a href=\"wap_health_26.html\" title=\"手机健康WAP导航\">健康</a></li>"+
			"<li><a href=\"wap_women_27.html\" title=\"手机女性WAP导航\">女性</a></li>"+
			"<li><a href=\"wap_buy_28.html\" title=\"手机购物WAP导航\">购物</a></li>"+
			"<li><a href=\"wap_review_29.html\" title=\"手机点评WAP导航\">点评</a></li>"+
			"<li><a href=\"wap_job_30.html\" title=\"手机招聘WAP导航\">招聘</a></li>";
	   //电脑网络 id=3
	  String content3=
			"<li><a href=\"wap_twitter_5.html\" title=\"手机微博WAP导航\">微博</a></li>"+
			"<li><a href=\"wap_soft_6.html\" title=\"手机软件WAP导航\">软件</a></li>"+
			"<li><a href=\"wap_email_11.html\" title=\"手机邮箱WAP导航\">邮箱</a></li>"+
			"<li><a href=\"wap_space_31.html\" title=\"手机空间WAP导航\">空间</a></li>"+
			"<li><a href=\"wap_hardware_32.html\" title=\"手机硬件WAP导航\">硬件</a></li>"+
			"<li><a href=\"wap_theme_33.html\" title=\"手机主题WAP导航\">主题</a></li>"+
			"<li><a href=\"wap_crack_40.html\" title=\"手机破解WAP导航\">破解</a></li>"+
			"<li><a href=\"wap_wapurl_41.html\" title=\"手机网址WAP导航\">网址</a></li>"+
			"<li><a href=\"wap_resources_43.html\" title=\"手机资源WAP导航\">资源</a></li>";
		
		
		
		String typeurl = "http://123.pctowap.com/";
		String select = "a";
		try {
			List<KsNavigate> list = new ArrayList<KsNavigate>();
			
			Document doc1 = Jsoup.parse(content1);
				//Jsoup.parse(content);
							//Jsoup.connect(typeurl).timeout(20000).get();
			
			Elements eles1 = doc1.select(select);
			
			for(Element t : eles1){
				KsNavigate g = new KsNavigate();
				String linkHref = t.attr("href");  
				String linkText = t.text();  
				
				g.setName(linkText);
				g.setParentid(1l);
				g.setStatus(2);
				g.setOrdernum(1);
				g.setUrl(typeurl+linkHref);
				list.add(g);
			}
			
			Document doc2 = Jsoup.parse(content2);
			Elements eles2 = doc2.select(select);
			
			for(Element t : eles2){
				KsNavigate g = new KsNavigate();
				String linkHref = t.attr("href");  
				String linkText = t.text();  
				
				g.setName(linkText);
				g.setParentid(2l);
				g.setStatus(2);
				g.setOrdernum(1);
				g.setUrl(typeurl+linkHref);
				list.add(g);
			}
			
			Document doc3 = Jsoup.parse(content3);
			Elements eles3 = doc3.select(select);
			
			for(Element t : eles3){
				KsNavigate g = new KsNavigate();
				String linkHref = t.attr("href");  
				String linkText = t.text();  
				
				g.setName(linkText);
				g.setParentid(3l);
				g.setOrdernum(1);
				g.setStatus(2);
				g.setUrl(typeurl+linkHref);
				list.add(g);
			}
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	
	}
	
	/**
	 * 抓取导航类型下的站点
	 * @param url
	 * @param type
	 * @return
	 */
	public static List<KsWebsite> crawlWebsite(String url,long navigateid){
		
		
		String prefurl = "http://123.pctowap.com/";
		String select = "div.famSiteBox ul li form a";
		
		try {
			Document doc = Jsoup.connect(url).timeout(20000).get();
			List<KsWebsite> list = new ArrayList<KsWebsite>();
			if(doc != null){
				Elements eles = doc.select(select);
				for(Element t : eles){
					
					KsWebsite g = new KsWebsite();
					
					String linkHref = t.attr("href");
					String linkText = t.text();        
					
					g.setLogo("");
					g.setName(linkText);
					g.setNavigateid(navigateid);
					g.setRecommend(1);
					g.setStatus(2);
					g.setUrl(prefurl+linkHref);
					g.setOrdernum(1);
					list.add(g);
				}
			}
			
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
}
